<template>
  <a-spin tip="Loading..." :spinning="spinning">

    <div>
      <a-popconfirm title="是否手动执行配股" ok-text="是" cancel-text="否" @confirm="confirm" @cancel="cancel">
        <a-button type="primary" style="margin: 20px 0 30px 50px;"> 手动执行配股</a-button>

      </a-popconfirm>

      <!--引用表格-->
      <a-tabs v-model:activeKey="activeKey" @change="change" style="margin: 0 30px;">
        <a-tab-pane key="1" tab="用户权益包">
          <a-table :columns="columns1" :data-source="dataSource1" bordered :pagination="pagination"
            @change="handleTableChange">
            <template #bodyCell="{ column, text }">
              <template v-if="column.dataIndex === 'name'">
                <a>{{ text }}</a>
              </template>
            </template>

          </a-table>
        </a-tab-pane>
        <a-tab-pane key="2" tab="商家权益包" force-render :pagination="pagination" @change="handleTableChange1">
          <a-table :columns="columns1" :data-source="dataSource2" bordered>
            <template #bodyCell="{ column, text }">
              <template v-if="column.dataIndex === 'name'">
                <a>{{ text }}</a>
              </template>
            </template>
          </a-table>
        </a-tab-pane>

      </a-tabs>


      <!-- <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" @click="allotment" preIcon="ant-design:plus-outlined"> 手动执行配股</a-button>
        <a-button type="primary" v-auth="'shipMember:ship_member:add'" @click="handleAdd"
          preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" v-auth="'shipMember:ship_member:importExcel'"
          preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'shipMember:ship_member:deleteBatch'">批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>

      </template>


    </BasicTable> -->

      <!-- 表单区域 -->
      <ShipMemberModal @register="registerModal" @success="handleSuccess"></ShipMemberModal>
    </div>
  </a-spin>

</template>

<script lang="ts" name="shipMember-shipMember" setup>
import { ref, reactive, computed, unref, onMounted } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage'
import ShipMemberModal from './components/ShipMemberModal.vue'
import { columns, searchFormSchema, superQuerySchema } from './ShipMember.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, performTheRightsIssueManually, manuallyExecuteTheAllocationOfEquityPackages } from './ShipMember.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { useUserStore } from '/@/store/modules/user';
onMounted(() => {
  getList('user', 1)
});
const pagination = ref(
  {
    current: 1,       // 当前页码
    total: 0,         // 总数据量
  }
)
const spinning = ref<boolean>(false);
const queryParam = reactive<any>({});
const checkedKeys = ref<Array<string | number>>([]);
const userStore = useUserStore();
//注册model
const [registerModal, { openModal }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: 'ship_member',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "ship_member",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
//手动执行配股
const allotment = () => {
  spinning.value = true;
  Promise.all([
    performTheRightsIssueManually(handleSuccess),
  ]).then(res => {
    spinning.value = false;
    getList('user', 1)

  })

}
// 高级查询配置
const superQueryConfig = reactive(superQuerySchema);
const dataSource1 = reactive([])
const dataSource2 = reactive([])
const columns1 = reactive([{
  title: '姓名',
  dataIndex: 'realname',
  key: 'realname',
  align: 'center'
},
{
  title: '手机号',
  dataIndex: 'phone',
  key: 'phone',
  align: 'center'
},
{
  title: '三级权益包',
  dataIndex: 'threeEquity',
  key: 'threeEquity',
  align: 'center'
},
{
  title: '二级权益包',
  dataIndex: 'towEquity',
  key: 'towEquity',
  align: 'center'
},
{
  title: '一级权益包',
  dataIndex: 'oneEquity',
  key: 'oneEquity',
  align: 'center'
},
{
  title: '特级权益包',
  dataIndex: 'maxEquity',
  key: 'maxEquity',
  align: 'center'
},
{
  title: '商家补贴权益包',
  dataIndex: 'merchantSubsidyEquity',
  key: 'merchantSubsidyEquity',
  align: 'center'
},
{
  title: '月供权益包',
  dataIndex: 'monthlyPaymentEquity',
  key: 'monthlyPaymentEquity',
  align: 'center'
},
{
  title: '工资权益包',
  dataIndex: 'salaryEquity',
  key: 'salaryEquity',
  align: 'center'
},
{
  title: '募捐权益包',
  dataIndex: 'fundraisingEquity',
  key: 'fundraisingEquity',
  align: 'center'
},
])

const handleTableChange = (e) => {
  pagination.value.current = e.current;
  dataSource1.splice(0, dataSource1.length);
  getList('user', e.current)
}
const handleTableChange1 = (e) => {
  pagination.value.current = e.current;
  dataSource1.splice(0, dataSource1.length);
  getList('shop', e.current)
}
const getList = (type, pageSize) => {

  if (type == 'user') {
    list({ userType: 1, pageNo: pageSize }).then(res => {
      Object.assign(dataSource1, res.records)
      pagination.value.total = res.total

    })
  } else {

    let username = JSON.parse(localStorage.getItem('userInfo')).username
    if (username == 'system') {

      list({ userType: 2, pageNo: pagination.value.current }).then(res => {
        pagination.value.total = res.total
        Object.assign(dataSource2, res.records)
      })
    } else {

      list({ userType: 2, pageNo: pagination.value.current, username: username }).then(res => {
        pagination.value.total = res.total
        Object.assign(dataSource2, res.records)
      })
    }

  }
}
const confirm = (e: MouseEvent) => {
  allotment()
};

const cancel = (e: MouseEvent) => {
  console.log(e);

};
const change = (e) => {
  pagination.value.current = 1
  if (e == 2) {
    getList('shop', 1)
  } else {
    getList('user', 1)
  }

}

const activeKey = ref('1');
/**
 * 高级查询事件
 */
function handleSuperQuery(params) {
  Object.keys(params).map((k) => {
    queryParam[k] = params[k];
  });
  reload();
}
/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
*/
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 操作栏
   */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'shipMember:ship_member:edit'
    }
  ]
}
/**
   * 下拉操作栏
   */
function getDropDownAction(record) {
  //  {
  //     label: '详情',
  //     onClick: handleDetail.bind(null, record),
  //   },
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
        placement: 'topLeft',
      },
      auth: 'shipMember:ship_member:delete'
    }
  ]
}



</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}
</style>